package com.yuelife.controller;

import com.yuelife.dto.IdDTO;
import com.yuelife.dto.SendOrderDTO;
import com.yuelife.service.HelpSendOrderService;
import com.yuelife.service.SendGoodService;
import com.yuelife.service.SendPriceItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author by Rxzeg
 * @date 2020-07-01 22:15
 * @describe
 */
@Controller
@CrossOrigin
@RequestMapping("/helpsend")
public class HelpSendController {
    @Autowired
    private SendPriceItemsService sendPriceItemsService;
    @Autowired
    private SendGoodService sendGoodService;
    @Autowired
    private HelpSendOrderService helpSendOrderService;
    @PostMapping("/addorder")
    @Transactional
    @ResponseBody
    public Map createdOrder(SendOrderDTO sendOrderDTO){
     Map map =  new HashMap<>();
        System.out.println(sendOrderDTO.toString());
         String order_id= UUID.randomUUID().toString();
         IdDTO idDTO=new IdDTO();//用来前两个数据表自动生成的id
        try {
            //添加配送费明细表 需要订单表订单id 可以利用uuid生成后传过去
            sendPriceItemsService.add(sendOrderDTO,order_id,idDTO);
            //添加配送商品表 需要订单表订单id
            sendGoodService.add(sendOrderDTO,order_id,idDTO);
            //添加订单
            helpSendOrderService.add(sendOrderDTO,order_id,idDTO);
            map.put("code",200);
        } catch (Exception e) {
            map.put("code",500);
        }
     return map;
    }
}
